package com.yuwj.chinaa.activity;

import android.content.Context;
import android.content.Intent;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.yuwj.chinaa.R;
import com.yuwj.chinaa.adapter.NewsAdapter;
import com.yuwj.chinaa.base.BaseActivity;
import com.yuwj.chinaa.entity.EntityNews;
import com.yuwj.chinaa.entity.EntityPage;
import com.yuwj.chinaa.presenter.IPresenter;
import com.yuwj.chinaa.presenter.presenterImpl.PresenterImpl;
import com.yuwj.chinaa.util.ToastUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class NewsListActivity extends BaseActivity {


    List<EntityNews> entityNewsList;
    NewsAdapter newsAdapter;
    IPresenter presenter;
    @BindView(R.id.rv_list)
    RecyclerView rvList;
    @BindView(R.id.srl_list)
    SwipeRefreshLayout srlList;

    EntityPage entityPage;
    boolean isRefresh;

    public static Intent createIntent(Context context) {
        return new Intent(context, NewsListActivity.class);
    }

    @Override
    public int setCustomContentViewResourceId() {
        return R.layout.activity_news;
    }

    @Override
    public void initParam() {
        entityPage = new EntityPage();
        presenter = new PresenterImpl(this);
        entityNewsList = new ArrayList<>();
        newsAdapter = new NewsAdapter(context, entityNewsList);
    }

    @Override
    public void initView() {
        rvList.setAdapter(newsAdapter);
        srlList.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                refresh();
            }
        });
        newsAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                loadMore();
            }
        }, rvList);
        rvList.setLayoutManager(new LinearLayoutManager(context));
        newsAdapter.setEmptyView(R.layout.content_recyclerview_empty, (ViewGroup) rvList.getParent());
        newsAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                startActivity(NewsDetailActivity.createIntent(context, (EntityNews) adapter.getItem(position)));
            }
        });
        newsAdapter.disableLoadMoreIfNotFullPage();
    }

    @Override
    public void initOperator() {
        refresh();
    }

    private void refresh() {
        srlList.setRefreshing(false);
        isRefresh = true;
        entityPage.setPageNo(0);
        JSONObject jsonObject = new JSONObject();
        try {
            switch (entityUser.getUserType()){
                case 20030002://普通
                    jsonObject.put("type", 20090001+"");//普通会员介绍：20090001
                    break;
                case 20030001://合伙人
                    jsonObject.put("type", 20090002+"");//合伙人介绍：20090002
                    break;
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        presenter.getNewsList(jsonObject.toString());

    }

    private void loadMore() {
    }



    @Override
    public void onGetNewsListSuccess(List<EntityNews> entityNewsList) {
        super.onGetNewsListSuccess(entityNewsList);
        if (entityNewsList!=null && entityNewsList.size()>0){
            this.entityNewsList.clear();
            this.entityNewsList.addAll(entityNewsList);
            newsAdapter.notifyDataSetChanged();
        }
    }

    @Override
    public void onGetNewsListFail(Exception e) {
        super.onGetNewsListFail(e);
        ToastUtil.getInstance().shortLength(context,e.getMessage());
    }
}
